function requestTestResults(path){
//	$("#layer-processing").stop().hide().css('opacity',1);
//	$("#layer-processing").delay(1000).fadeIn(400);
	$.ajax({
		url: "./?action=test",
		data: {
			path: path,
			format: "xml"
		},
		type: "post",
		dataType: "xml",
/*		complete: function(data){
			console.log(data);
		},
*/		success: function(data){
			$("#layer-processing").hide().stop(true,true);
			var root	= $(data.childNodes[0]);
			var suite	= root.children("testsuite");
			var total	= parseInt(suite.attr("tests"));
			var passed	= total - parseInt(suite.attr("errors")) - parseInt(suite.attr("failures"));
			var table	= $("table");
			console.log('total: '+total);
			console.log('passed: '+passed);
			table.find("tr:eq(0)>td").html(suite.attr("name"));
			table.find("tr:eq(1)>td").html(renderIndicator(passed,total));
			table.find("tr:eq(2)>td:eq(0)").html(suite.attr("tests")+" <small>("+suite.attr("assertions")+")</small>");
			table.find("tr:eq(2)>td:eq(1)").html(suite.attr("errors")+" Error(s)");
			table.find("tr:eq(2)>td:eq(2)").html(suite.attr("failures")+" Failure(s)");
			var key = suite.attr("name").replace(/\//g,'_');
			console.log("key: "+key);
			$("li#node-"+key).css('background-color',calculateStatusColor(passed/total,0.75));
//			table.css('background-color',calculateStatusColor(passed/total,0.75));
//			table.css('background-color','red');
			console.log(suite);
			console.log(suite.attr("name"));
//			console.log(suite.find('testcase failure').parent().attr('name'));

			var errors		= $("<ul></ul>").addClass('test-errors');
			var failures	= $("<ul></ul>").addClass('test-failures');
			suite.find('testcase').each(function(nr){
				var testcase = $(this);
				testcase.find("error").each(function(){
					var line = $("<span></span>").addClass('line').html(' :: '+testcase.attr('line'));
					var labelClass = $("<span></span>").addClass('item-class').html(testcase.attr('class'));
					var labelMethod = $("<span></span>").addClass('item-method').html(testcase.attr('name'));
					var labelLine = $("<span></span>").addClass('item-line').html(testcase.attr('line'));
					label = $("<div></div>").addClass("item-label").append(labelClass).append(labelMethod).append(labelLine);
					var text = $.trim($(this).text().replace(/\n.+phpunit:[0-9]+/,''));
					var code = "<pre>"+text+"</pre>";
					var item = $("<li></li>").append(label).append(code);
					errors.append(item);
				});
				testcase.find("failure").each(function(){
					var line = $("<span></span>").addClass('line').html(' :: '+testcase.attr('line'));
					var labelClass = $("<span></span>").addClass('item-class').html(testcase.attr('class'));
					var labelMethod = $("<span></span>").addClass('item-method').html(testcase.attr('name'));
					var labelLine = $("<span></span>").addClass('item-line').html(testcase.attr('line'));
					label = $("<div></div>").addClass("item-label").append(labelClass).append(labelMethod).append(labelLine);
					var text = $.trim($(this).text().replace(/\n.+phpunit:[0-9]+/,''));
					text = text.replace(/.+\n/,'');
					var code = "<pre>"+text+"</pre>";
					var item = $("<li></li>").append(label).append(code);
					failures.append(item);
				});
				if(errors.find('li').size()){
					errorsBlock = $("<div></div>").html(errors);
					errorsBlock.prepend($("<h2></h2>").html('Errors'));
				}
				else
					errorsBlock = "";
				if(failures.find('li').size()){
					failuresBlock = $("<div></div>").html(failures);
					failuresBlock.prepend($("<h2></h2>").html('Failures'));
				}
				else
					failuresBlock = "";
				$("#test-errors").html(errorsBlock);
				$("#test-failures").html(failuresBlock);
			});

//			console.log(data);
			
//			console.log();
//			var x = new UnitTestResultXml(data);
//			console.log(x.getErrorCount());
		}
	});

}
function calculateStatusColor(ratio){
	ratio	= Math.max(Math.min(ratio,1),0);
	console.log('ratio: '+ratio);
	var red		= Math.round((1 - ratio) > 0.5 ? 255 : (1 - ratio) * 2 * 255);
	var green	= Math.round(ratio > 0.5 ? 255 : (ratio * 2 * 255));
	var blue	= 0;
	var hex		= red.toString(16).pad(2,0,1)+green.toString(16).pad(2,0,1)+blue.toString(16).pad(2,0,1);
	console.log('red: '+red);
	console.log('green: '+green);
	console.log('blue: '+blue);
	console.log('hex: '+hex);
	return '#'+hex;
}


function renderIndicator(points, total){
	var ratio = points / total;
	var track = $("<div></div>").addClass('indicator-track');
	var bar = $("<div></div>").addClass('indicator-bar').css('width',Math.round(ratio * 100)+'%');
	bar.css('background-color',calculateStatusColor(ratio,0));
	return track.append(bar);
}

function UnitTestResultXml(xml){
	this.xml = xml;
	
	this.getErrorCount = function(){
		return this.xml;
//		return $this->tree->testsuite[0]->getAttribute( "errors" );
	}
}
